#! /usr/bin/python
# -*- coding: UTF-8 -*-

class events():
    def __init__(self, parent):
        self.parent = parent
        self.Log = self.parent.Log

    def OnRefreshClick(self, event):
        self.Log('refreshing')
        self.parent.statuses.RefreshStatusWidget()
            
    def OnDisconnectClick(self, event):
        self.parent.opt_twitter_username = ''
        self.parent.opt_twitter_password = ''
        self.parent.twit_api.set_auth(self.parent.opt_twitter_username, self.parent.opt_twitter_password)
    
    def OnOptionsClick(self, event):
        # TODO: save to disk or sql, show better modal
        # Show Options modal with username and password
        dlg = wx.TextEntryDialog(None, "Username", 'Twitter options', self.parent.opt_twitter_username)
        dlg2 = wx.TextEntryDialog(None, "Password", 'Twitter options', self.parent.opt_twitter_password)
        if dlg.ShowModal() == wx.ID_OK:
            self.parent.opt_twitter_username = dlg.GetValue().encode('utf8')
        if dlg2.ShowModal() == wx.ID_OK:
            self.parent.opt_twitter_password = dlg2.GetValue().encode('utf8')
        self.parent.ConfigSet('twitter_username', self.parent.opt_twitter_username, 'Twitter')
        self.parent.ConfigSet('twitter_password', self.parent.opt_twitter_password, 'Twitter')
        self.parent.twit_api.set_auth(self.parent.opt_twitter_username, self.parent.opt_twitter_password)
        
    def OnTweetEnter(self, event):
        tweet = event.GetString().encode('utf8')
        self.parent.tweet_box.Clear()
        self.parent.twit_api.status_update(tweet)
        self.parent.statuses.RefreshStatusWidget()

    def OnReplyButtonClick(self, event):
        id = event.GetId()        
        if id in self.parent.tweet_users:
            in_reply_to_user_name = self.parent.tweet_users[id]
            self.parent.tweet_box.SetValue('@'+in_reply_to_user_name+' ')
            self.parent.tweet_box.SetFocus()

    def OnLinkClicked(self, link):
        wx.LaunchDefaultBrowser(link.GetHref())

    def OnAboutClick(self, event):
        dlg = wx.MessageDialog(None, __ABOUT__, 'About', wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def OnHelpClick(self, event):
        dlg = wx.MessageDialog(None, __HELP__, 'Help', wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def OnTaskBarLeftClick(self, event):
        if not self.parent.IsShown():
            self.parent.Show()
        else:
            self.parent.Iconize()
            self.OnIconize(event)
        
    def OnTaskBarRightClick(self, event):
        self.OnExitClick(event)

    def OnKeyDown(self, event):
        key = event.GetKeyCode()
        self.Log(key)
        ctrl = event.ControlDown()
        alt = event.AltDown()
        shift = event.ShiftDown()
        if key == ord('X') and ctrl:
            self.Log('cut')
        elif key == wx.WXK_F5:
            sel.OnRefreshClick()
        event.Skip()

    def OnIconize(self, event):
        if self.parent.IsIconized():
            self.parent.Hide()
        
    def OnExitClick(self, event):
        self.parent.Close(True);
        wx.GetApp().ProcessIdle()